<template>
<div>
<el-row>

<el-form :inline="true" >
	<el-form-item >
		<daterange v-bind:se.sync="s_e" type="daterange"></daterange>
    </el-form-item>

	<el-form-item>
	    <el-button type="primary" @click="onSubmit" :loading="btnloading">查询</el-button>
	</el-form-item>

	<el-form-item>
	    <el-button type="success" @click="toExcel">导出Excel</el-button>
	</el-form-item>

</el-form>
</el-row>

<el-row>
<el-table
    :data="objs"
    border
    v-loading="loading"
    style="width: 100%">
    

    <el-table-column
      prop="count"
      label="一级数量"
      width="300"
      sortable
      >
    </el-table-column>

   
    
</el-table>
</el-row>

</div>
</template>

<script>

import { mapGetters } from 'vuex'
import { date2str, dateDiff } from '../../../config/util'
import daterange from '../../../components/daterange'

export default {
	
	name : 'sellerreport',
	
	data () {
		return {
		  	s_e : [date2str(new Date()), date2str(new Date())],
		  	loading : false,
		  	filename : '',

		  	btnloading : false,

		  	sellers : [],
		}
	},

	props : {

	},

	components : {
		daterange,
	},

	computed : {
		...mapGetters([
			// 'book',
			// 'userInfo',
		]),

		objs () {
			var arr = [
				{
					count : this.sellers.length,
				}
			]

            return arr;
		},
		
	},

	methods : {
		getData() {

			const day = dateDiff(this.s_e[0], this.s_e[1]);
			
			if(day > 30){
                this.$message.warning('时间跨度不能超过30天');
                return;
			}

			this.loading = true;
			this.btnloading = true;

			this.filename = '分销商报表' + '[' + this.s_e[0] + '~' + this.s_e[1] + ']';

			let para = {
				fun : 'sellerreport',
				start_time : this.s_e[0] + ' 00:00:00',
				end_time : this.s_e[1] + ' 23:59:59',
				cb : res => {
					this.sellers = res;
					this.loading = false;
					this.btnloading = false;
				},
				err : res => {
					this.loading = false;
					this.btnloading = false;
					this.$notify.error({
			          title: '错误',
			          message: res,
			        });
				}
			};
			this.$store.dispatch('update', para);
		},

		onSubmit () {
			this.getData();
		},

		toExcel (){
			
			const labels = ['nickname', 'member_levelname', 'fx_levelname', 'remark',
        	'update_time' ];

	        let arr = [];
	        for(let i = 0; i< this.sellers.length; i++){
	            let tmp = this.sellers[i];
	            let haha = [];
	            for(let j = 0; j < labels.length; j++){
	                haha.push(tmp[labels[j]]);
	            }
	            arr.push(haha);
	        }
	        arr.splice(0, 0, ['昵称', '分销体系', '级别' , '备注', 
            '时间' ]);

	        ExportUtil.toExcel(arr, this.filename);

		}
	},

	watch : {
	    
	},

	mounted () {
		this.getData();
	},

}
</script>

<style scoped>

</style>
